import request from "@/utils/request";
import {
  type HomeQueryParams,
  ListResult,
  OrderFieldItem,
  DelResult,
  SortDataParam,
  OrderDetail,
} from "./types";
import { AxiosPromise } from "axios";

/**
 * 获取工单列表
 *
 * @param params
 */
export function getOrderList(
  params: HomeQueryParams
): AxiosPromise<ListResult<string>> {
  return request({
    url: "/api/order/index",
    method: "get",
    params: params,
  });
}
/**
 * 获取自定义字段列表
 *
 * @param params
 */
export function getOrderField(
  params: HomeQueryParams
): AxiosPromise<ListResult<OrderFieldItem>> {
  return request({
    url: "/api/order_field/index",
    method: "get",
    params: params,
  });
}
// 获取工单详情
export const getOrderInfo = (
  params: any
): AxiosPromise<DetailResult<OrderDetail<OrderFieldItem>>> => {
  return request({
    method: "get",
    url: "/api/order/info",
    params,
  });
};
// 添加工单
export const addOrder = (data: any): AxiosPromise<DelResult> => {
  return request({
    method: "post",
    url: "/api/order/add",
    data,
  });
};
// 编辑工单
export const editOrder = (
  id: number | string,
  data: any
): AxiosPromise<DelResult> => {
  return request({
    method: "post",
    url: `/api/order/edit/id/${id}`,
    data,
  });
};

/**
 * 自定义字段排序
 *
 * @param params
 */
export const sortField = (data: SortDataParam): AxiosPromise<DelResult> => {
  return request({
    method: "post",
    url: `api/order_field/sortField`,
    data,
  });
};

/**
 * 新增自定义字段
 *
 * @param params
 */
// 新增自定义字段 api/order_field/add 参数看文档
export const addOrderField = (data: any) => {
  return request({
    method: "post",
    url: "api/order_field/add",
    data,
  });
};

/**
 * 编辑自定义字段
 *
 * @param params
 */
// 编辑自定义字段 api/order_field/add 参数看文档
export const editOrderField = (id: number, data: any): AxiosPromise<any[]> => {
  return request({
    method: "post",
    url: `api/order_field/edit/id/${id}`,
    data,
  });
};

/**
 * 删除自定义字段
 *
 * @param params
 */
export const delOrderField = (
  ids: string,
  params: any
): AxiosPromise<DelResult> => {
  return request({
    method: "get",
    url: `/api/order_field/del/ids/${ids}`,
    params,
  });
};

/**
 * 删除工单
 * @param ids {string} 工单id数组
 */
export function delOrder(ids: string, params: any): AxiosPromise<DelResult> {
  return request({
    url: `/api/order/del/ids/${ids}`,
    method: "get",
    params: params,
  });
}

/**
 * 获取材料详情
 * @param ids {string} 工单id数组
 */
export function getMaterialList(
  params: HomeQueryParams
): AxiosPromise<ListResult<OrderFieldItem>> {
  return request({
    url: "/api/order_material/index",
    method: "get",
    params: params,
  });
}
